//
//  NetworkAPI.swift
//  fho
//
//  Created by 郭颢源 on 2021/4/16.
//

import UIKit
import Moya
import HandyJSON
import RxCocoa
import RxSwift

let NetworkProvider = MoyaProvider<NetworkAPI>(plugins: [networkPlugin])

enum NetworkAPI {
    // 实时天气
    case realtimeWeather(cityId:String)
//    case getCode(cityId:String)
}

extension NetworkAPI:TargetType{
    public var baseURL: URL{
        switch self {
        case .realtimeWeather:
            return URL(string: "https://zhizhu.56114.com/lis/danhao/index1?type=1")!
        }
    }
    
    // 对应的不同API path
    var path: String {
        switch self {
        case .realtimeWeather: return ""
        }
    }
    
    // 请求类型
    public var method: Moya.Method {
        return .get
    }
    
    // 请求任务事件（这里附带上参数）
    public var task: Task {
        var parmeters: [String : Any] = [:]
        switch self {

        case .realtimeWeather(let cityId):
            parmeters = ["cityId":cityId] as [String : Any]
            return .requestParameters(parameters: parmeters, encoding: URLEncoding.default)
        }
    }
    
    // 是否执行Alamofire验证
    public var validate: Bool {
        return false
    }
    
    // 这个就是做单元测试模拟的数据，只会在单元测试文件中有作用
    public var sampleData: Data {
        return "{}".data(using: String.Encoding.utf8)!
    }
    
    // 请求头
    public var headers: [String: String]? {
        return nil
    }
    
    
}


//let networkPlugin = NetworkActivityPlugin { (activityType, targetType) in
//    switch activityType{
//    
//    case .began:
//        JSShowMessage.showTip(msg: "加载中...")
//        print("显示loading")
//    case .ended:
//        JSShowMessage.hidenTip()
//        print("隐藏loading")
//    }
//}



/**
let provider = MoyaProvider<NetworkAPI>(plugins: [networkPlugin])
//provider.plugins = [networkPlugin]

provider.request(.realtimeWeather(cityId: "")) { (result) in

}



NetworkProvider.request(NetworkAPI.realtimeWeather(cityId: "101040100")) { result in
    if case .success(let response) = result {
        // 解析数据
        let jsonDic = try! response.mapJSON() as! NSDictionary
        print(jsonDic)
    }
}
 */
